﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SaveExcelReport.aspx.cs" Inherits="PreContractWeb.Report.SaveExcelReport" %>

<html>
    <head>
    </head>
    <body>
        <input type="hidden" id="msg" name="msg" runat="server" />
        <input type="hidden" id="RptID" name="RptID" runat="server" />
        <input type="hidden" id="RptDate" name="RptDate" runat="server" />
        <input type="hidden" id="RptType" name="RptType" runat="server" />        
        <script language="javascript" >
           
            var lblError=null;
            //alert(RptType.value);
            if (RptType.value=="Monthly")
                lblError=window.parent.document.getElementById("ctl00_ContentPlaceHolder1_divError");
            else
                lblError=window.parent.document.getElementById("ctl00_ContentPlaceHolder1_divError_Quarterly");
                
            if (msg.value=="")
            {
                alert("报表保存成功!\nReport has been saved successfully!");
                //Update History dropdown list
                
                UpdateHistRptList();
                
                //alert(RptID.value);
                //alert(RptDate.value);
                
            }
            else if (msg.value =="TimeOut")
            {
                alert("会话过期,请重新运行报表并保存！\nCurrent session expired, please run the report and save again!");   
                //window.parent.parent.location.href="../login.aspx";           

            }
            else
            {
                alert("报表保存失败!\nFailed to save report!");                
            }
            if (lblError!=null)
                lblError.innerHTML="";                
        
        
            function UpdateHistRptList()
            {
                //alert(RptType.value);
                var objSel=null;
                if (RptType.value=="Monthly")
                    objSel=window.parent.document.getElementById("ctl00_ContentPlaceHolder1_drRpt");
                else
                    objSel=window.parent.document.getElementById("ctl00_ContentPlaceHolder1_drRpt_Quarterly");

                var n=0;
                
                //Check if the report exists or not
                if (objSel!=null)
                {
                    for (var i=0;i<objSel.length;i++)
                    {
                        if (objSel.options[i].value==RptID.value)
                        {
                            n=1;
                            break;
                        }                  
                    }
                    var opt=new Option(RptDate.value,RptID.value, false);
                    
                    //Not found, need to add to dropdownlist
                    if (n==0) 
                    {
                        var dt;
                        //var dtRpt=new Date(RptDate.value);
                        var dtRpt=RptDate.value;
                        n=0;
                        
                        //Insert the report to list by date sequence
                        for (var i=1;i<objSel.length;i++)
                        {
                            //dt=new Date(objSel.options[i].text);
                            dt=objSel.options[i].text;

                            if (dt<dtRpt)
                            {
                                n=1;
                                objSel.add(opt,i);
                                break;
                            }
                            
                        }
                        
                        //add to the end of the list
                        //alert(n);
                        
                        if (n==0 && objSel.length<16)
                            objSel.add(opt);
                            
                        
                        //keep the list to 15 items
                        if (objSel.length>16)
                        {
                            for (var i=16;i<objSel.length;i++)
                            {
                                objSel.remove(i);
                                i--;
                            }
                        }

                        objSel.selectedIndex=0;
                    
                    }
                }
            
            }
        </script>
    </body>
</html>